package com.inno.auth.domain.utils;

import cn.hutool.json.JSONException;
import cn.hutool.json.JSONObject;
import com.inno.auth.domain.properties.WeChatProperties;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

/**
 * @author sean
 */
@Component
public class WechatUtil {
    @Resource
    private WeChatProperties weChatProperties;
    public String getOpenId(String code) {
        Map<String, String> map = new HashMap<>();
        map.put("appid", weChatProperties.getAppid());
        map.put("secret", weChatProperties.getSecret());
        map.put("js_code", code);
        map.put("grant_type", weChatProperties.getGrantType());

        try {
            String json = HttpClientUtil.doGet(weChatProperties.getGetOpenIdUrl(), map);
            JSONObject jsonObject = new JSONObject(json);
            return jsonObject.getStr("openid");
        } catch (JSONException e) {
            throw new RuntimeException(e);
        }
    }
}
